package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.entity.vo.CourseQuery;
import com.atguigu.eduservice.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author wangshuai
 * @since 2021-02-04
 */
@Api(description = "课程管理controller")
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;

    //课程列表,分页带查询条件
    @PostMapping("getPageCourseList/{page}/{limit}")
    public R getPageCourseList(@RequestBody(required = false) CourseQuery courseQuery, @PathVariable Long page, @PathVariable Long limit) {

        Map<String,Object> map = eduCourseService.getAllCoursePageList(page,limit,courseQuery);

        return R.ok().data("data",map);
    }

    @PostMapping("addCourse")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {

        String id = eduCourseService.addCourseInfo(courseInfoVo);

        return R.ok().data("courseId", id);
    }

    @GetMapping("getCourseDetail/{courseId}")
    public R getCourseDetail(@PathVariable String courseId) {

        CourseInfoVo courseInfoVo = eduCourseService.showDetail(courseId);

        return R.ok().data("data",courseInfoVo);
    }

    @PostMapping("updateCourse")
    public R updateCourse(@RequestBody CourseInfoVo courseInfoVo) {

        String id = eduCourseService.updateCourseInfo(courseInfoVo);

        return R.ok().data("courseId", id);
    }

    //根据课程id查询最终发布信息
    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id) {

        CoursePublishVo coursePublish = eduCourseService.publishCourseInfo(id);

        return R.ok().data("data",coursePublish);
    }

    //课程最终发布，修改课程状态
    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id) {

        EduCourse eduCourse = new EduCourse();

        eduCourse.setId(id);

        eduCourse.setStatus("Normal");

        eduCourseService.updateById(eduCourse);

        return R.ok();
    }

    //删除课程，修改课程状态
    @PostMapping("deleteCourse/{id}")
    public R deleteCourse(@PathVariable String id) {

        Boolean flag = eduCourseService.deleteCourse(id);

        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

}

